<?php
/**
 * Display a page if the user has read access or is an admin.
 *
 * This is the default page handler used by Wikka when no other handler is specified.
 * Depending on user privileges, it displays the page body or an error message. It also
 * displays footer comments and a form to post comments, depending on ACL and general
 * config settings.
 *
 * @package		Handlers
 * @subpackage	Page
 * @version		$Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @filesource
 *
 * @uses		Wakka::Format()
 * @uses		Wakka::FormClose()
 * @uses		Wakka::FormOpen()
 * @uses		Wakka::GetConfigValue()
 * @uses		Wakka::GetPageTag()
 * @uses		Wakka::GetUser()
 * @uses		Wakka::GetUserName()
 * @uses		Wakka::HasAccess()
 * @uses		Wakka::Href()
 * @uses		Wakka::htmlspecialchars_ent()
 * @uses		Wakka::LoadComments()
 * @uses		Wakka::LoadPage()
 * @uses		Wakka::LoadUser()
 * @uses		Wakka::UserIsOwner()
 * @uses		Config::$anony_delete_own_comments
 * @uses		Config::$hide_comments
 *
 * @todo		move <div> to template;
 * 			necessary) - #312
 */

$debug_domains = array('klenwell', 'localhost');
$show_script_debug = in_array($_SERVER['SERVER_NAME'], $debug_domains);

//constants
define('SHOW_OLD_REVISION_SOURCE', 0); # if set to 1 shows by default the source of an old revision instead of the rendered version

//validate URL parameters
$raw = (!empty($_GET['raw']))? (int) $this->GetSafeVar('raw', 'get') : SHOW_OLD_REVISION_SOURCE;

?>
<!-- BEGIN PAGE CONTENT -->
<?php
echo '<div id="content"';
echo (($user = $this->GetUser()) && ($user['doubleclickedit'] == 'N') || !$this->HasAccess('write')) ? '' : ' ondblclick="document.location=\''.$this->Href('edit', '', 'id='.$this->page['id']).'\';" '; #268
echo '>'."\n"; //TODO: move to templating class

if (!$this->HasAccess('read'))
{
	echo '<p><em class="error">You aren\'t allowed to read this page.</em></p>';
	echo "\n".'</div><!--closing page content-->'."\n"; //TODO: move to templating class
}
else
{
	if (!$this->page)
	{
		echo '<p>This page doesn\'t exist yet. Maybe you want to <a href="'.$this->Href('edit').'">create</a> it?</p>';
		echo '</div><!--closing page content-->'."\n"; //TODO: move to templating class
	}
	else
	{
		if ($this->page['latest'] == 'N')
		{
			echo '<div class="revisioninfo">'."\n";
			echo '<h4>Revision ['.$this->page['id'].']</h4>'."\n";
			echo 'This is an <a href="'.$this->Href('revisions').'">old revision</a> of <a href="'.$this->Href().'">'.$this->GetPageTag().'</a> made by '.$this->page['user'].' on <span class="datetime">'.$this->page['time'].'</span>.'."\n";
?>
				<br />
				<?php echo $this->FormOpen('show', '', 'GET', '', 'left') ?>
				<input type="hidden" name="time" value="<?php echo $this->GetSafeVar('time', 'get') ?>" />
				<input type="hidden" name="raw" value="<?php echo ($raw == 1)? '0' :'1' ?>" />
				<input type="submit" value="<?php echo ($raw == 1)? 'Show formatted' : 'Show source' ?>" />&nbsp;
				<?php echo $this->FormClose(); ?>
<?php
			// if this is an old revision, display some buttons
			if ($this->HasAccess('write'))
			{
				// added if encapsulation : in case where some pages were brutally deleted from database
				if ($latest = $this->LoadPage($this->tag))
				{
?>
 					<?php echo $this->FormOpen('edit') ?>
 					<input type="hidden" name="previous" value="<?php echo $latest['id'] ?>" />
 					<input type="hidden" name="body" value="<?php echo $this->htmlspecialchars_ent($this->page['body']) ?>" />
 					<input type="submit" value="Re-edit this old revision" />
 					<?php echo $this->FormClose(); ?>
<?php
				}
			}
			echo '<div class="clear"></div></div>'."\n";
		}
		// display page
		if ($raw == 1)
		{
			echo '<div class="wikisource">'.nl2br($this->htmlspecialchars_ent($this->page["body"], ENT_QUOTES)).'</div>';
		}
		else
		{
			echo $this->Format($this->page['body'], 'wakka', 'page');
		}
?>
<div style="clear: both"></div>
</div>

<!-- Inserted Content -->
<?php
    /*
        This will insert content right before the comments section: in the
        klenwell template, that means top of the right column.

        To add content, simply add file feature_box.inc to wikka config dir.
    */
    $_wikka_dir = dirname(dirname(dirname(dirname(__FILE__))));
    $_config_dir = sprintf('%s/config', $_wikka_dir);

    $_home_page_box = sprintf('%s/%s', $_config_dir, 'home_page_box.inc');
    if ( $this->tag == 'HomePage' ) {
        @include($_home_page_box);
    }

    $_feature_box_file = sprintf('%s/%s', $_config_dir, 'feature_box.inc');
    @include($_feature_box_file);
?>

<!-- END PAGE CONTENT -->



<!-- COMMENTS -->
<?php
    require_once('libs/comment.lib.php');

    if ( $this->GetConfigValue('hide_comments') != 1 )
    {
        $show_comments = show_page_comments($this->GetPageTag(),
                                            $this->UserWantsComments());

        if ( $show_comments ) {
            $comment_block = build_comment_block($this);
        }
        else {
            $comment_block = build_collapsed_comment_block($this);
        }

        print $comment_block;
    }
?>
<!-- END COMMENTS -->



<?php
 // CLOSE BLOCKS AT TOP

    }
}
?>
